// Created by liusc on 2020/8/5-9:24.
import { getDict } from "~/utils";
import { getFiles, checkFiles } from "@/views/wy-budget/api/fileoperation.js";
import uploadMedia from '@/components/upload-media'
import BaseLoad from '@/assets/vue-mixin/BaseLoad'
import { getObj, getEditHis, getParameterItem2 } from "@/views/wy-budget/api/budgetparameter.js";
import fileUpload from '@/views/wy-budget/components/fileUpload'

export default {
    name: "apply_add",
    mixins: [BaseLoad],
    components: { uploadMedia, fileUpload },

    data () {
        return {
            //数据主键ID
            parameterId: '',
            // 文件ID
            fileId: '',
            applyFileId:'',
            applyFileId2:'',
            amendFileId:'',
            amendFileId2:'',
            isShow: false,
            fileList: [],
            modelType: 'budgetApply',
            busId: '',
            busId2: '',
            busId3: '',
            isBusFlag: false,
            isBusFlag2: false,
            isBusFlag3: false,
            editList: [],

            form: {
                applyData: {
                    parameterId: '',
                    applyId: '',
                    budgetId: '',
                    budgetCode: '',
                    applyName: '',
                    applyDept: '',
                    applyType: '',
                    budgetSum: '',
                    budgetSum2: '',
                    changeAmount: '',
                    remarks: '',
                    fileId: '',
                },
                itemValueList: [],
                operateType: '',
            },

            rules: {
            }

        }
    },

    computed: {
        applyDept2: function () {
            let types = getDict('budget_department')
            let pv = this.form.applyData.applyDept
            if (pv && types.length > 0) {
                const pts = types.filter(t => t.value == pv)
                return pts ? pts[0].label : ''
            }
            return ''
        },

        applyType2: function () {
            let types = getDict('budget_type')
            let pv = this.form.applyData.applyType
            if (pv && types.length > 0) {
                const pts = types.filter(t => t.value == pv)
                return pts ? pts[0].label : ''
            }
            return ''
        },
    },

    beforeMount() {
        this.parameterId = this.$route.query.parameterId
        this.fileId = this.$route.query.fileId
        this.applyFileId = this.$route.query.applyFileId
        this.amendFileId = this.$route.query.amendFileId
        this.applyFileId2 = this.$route.query.applyFileId2
        this.amendFileId2 = this.$route.query.amendFileId2

        const loading = this.$loading({
            lock: true,
            text: '数据加载中'
        });

        getObj(this.$route.query.parameterId).then(respData => {
            this.$refs['add-form'].resetFields()
            this.form.applyData = respData.data.data

            this.initItem();

            this.initEditList();

            this.busId = respData.data.data.fileId

            // if(this.busId){
            //     this.isBusFlag = true
            //     this.$refs.myApply.getFiles(this.busId)
            // }

            // this.busId2= respData.data.data.applyFileId
            // if(this.busId2){
            //     this.isBusFlag2 = true
            //     this.$refs.myApply2.getFiles(this.busId2)
            // }

            // this.busId3= respData.data.data.amendFileId
            // if(this.busId3){
            //     this.isBusFlag3 = true
            //     this.$refs.myApply3.getFiles(this.busId3)
            // }

            loading.close()
        }).catch(e => {
            loading.close()
            this.$message({
                type: 'error',
                message: '数据获取失败!'
            })
        })
    },

    created () {

    },

    methods: {
        // 科目
        initItem () {
            getParameterItem2(this.form.applyData.parameterId).then(rep => {
                let rspD = rep.data.data
                if (rspD) {
                    rspD = rspD.map((rs, index) => {
                        // rs['sortNum'] = index + 1
                        return rs
                    })
                }
                this.form.itemValueList = rspD
            })
        },

        // 编辑历史记录
        initEditList () {
            getEditHis(this.form.applyData.parameterId).then(rep => {
                let rspD = rep.data.data
                if (rspD) {
                    rspD = rspD.map((rs, index) => {
                        // rs['sortNum'] = index + 1
                        return rs
                    })
                }
                this.editList = rspD
            })
        },

        // 取消
        cancel () {
            this.closeNowRouter()
        },

        closeNowRouter () {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            //跳转
            this.$router.push({ name: 'parameter_list' })
        },

        handleDownload (fileId) {
            checkFiles(fileId).then(resp => {
                if (resp.data.code === 0) {
                    window.open('/admin/sys-file/download/' + fileId)
                } else {
                    this.$message({
                        message: `文件不存在！`,
                        type: 'warning'
                    })
                }
            })
        },
    }
}
